
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  property;


import pool.bean.Master_table_Bean;
import pool.bean.Property_details_Bean;
import utility.*;

import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Vector;

import logger.PoolLogger;
import User.Uservalidation;
import control.Control;
import control.RequestParameter;
import file.SearchRecord;
import database.RecordSearch;
import dbmanager.*;
import utility.Input;
import dbmanager.DBManager;

public class Codegenerator
{
	GetIdName gid;
	RecordSearch sr;
	PoolLogger pl;
	
	public Codegenerator() 
	{
		 gid = new GetIdName();
		 
		// logger
			pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	 
	 
	 public String findCode(String TypeCode)
	 {
		Hashtable<Object,Object>table = new Hashtable<Object, Object>();
		String path=Input.MASTERREPORTSPATH + TypeCode + "//" + TypeCode + "_pid" + Input.FILRFORMAT;
		int indexPV=-1;
		int indexVT=-1;
		try 
		{	
			indexPV = Column.pv_index-1;
			indexVT = Column.vt_index-1;
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			pl.info(e.getMessage());
			pl.debug(e.getMessage());
			pl.warn(e.getMessage());
			pl.fatal(e.getMessage());
			pl.error(e.getMessage());
			pl.trace(e.getMessage());
		}
		sr = new RecordSearch();
		table.put("pid", TypeCode);
		sr.setConditionMap(table);
		
		String line[] = sr.getArrayFromResultSet();
		
		 LinkedList< Object> code = new LinkedList<Object>();
		 for(int i=0;i<line.length;i++)
		 {
			 String Split[]=line[i].split("\t");
			 String Code = Split[indexPV];
			 if(Code.equals("0"))
				 code.add(Split[indexVT].toLowerCase());
			else
				code.add(gid.getItem(Code).toLowerCase());
		 }
		 System.out.println(code);
		 pl.info(code);
			pl.debug(code);
			pl.warn(code);
			pl.fatal(code);
			pl.error(code);
			pl.trace(code);
		 ItemCodeSequenceGenerator seq = new ItemCodeSequenceGenerator();
		 Vector<String> FinalSeq =  seq.seq;
		 for(int i=0;i<FinalSeq.size();i++)
		 {
			 String tempcode = FinalSeq.get(i).toLowerCase();
			 if(code.contains(tempcode)==false)
			 {
				 pl.info(tempcode);
					pl.debug(tempcode);
					pl.warn(tempcode);
					pl.fatal(tempcode);
					pl.error(tempcode);
					pl.trace(tempcode);
				return tempcode;
			 }
		 }
		/* 
		 for(int i=97;i<123;i++)
		 {
			 for(int j=97;j<123;j++)
			 {
				 for(int k=97;k<123;k++)
				 {
					 char temp =(char)i;
					 String tempcode= Character.toString(temp)+ (char)j + (char)k;
					 if(code.contains(tempcode)== false)
						{
					 		return tempcode;
						}		
				 }
			 }
		 }*/
		 
		 pl.info("Error");
			pl.debug("Error");
			pl.warn("Error");
			pl.fatal("Error");
			pl.error("Error");
			pl.trace("Error");
		 return "Error";
	 }
	 
	 public String RegisterItem(String code)
	 { 
		 
		 ChekInMaster chk = new ChekInMaster(); 
		 String finalcode=findCode(code);
		 
		 String userid = "+911234567891";
		 Uservalidation getpass = new Uservalidation();
		 String password = getpass.getPasword(userid);
		 
		 if(!chk.isInMaster(finalcode))
		 {
			 Master_table_Bean masterbean =new Master_table_Bean();
			 RequestParameter req = new RequestParameter();
			 req.setUsername(userid);
			 req.setMessage("System Message");
			 req.setPassword(password);
			 req.setRequest("master_table");
			 req.setProcess("add");				
			 masterbean.initPool();
			 masterbean.setReqParam(req);
			 masterbean.setProcess("add");
			 masterbean.setNewMasterObject(finalcode);
			 masterbean.insert();
		 }
		 
		 Property_details_Bean propertydetailsbean =new Property_details_Bean();
		 RequestParameter req = new RequestParameter();
		 req.setUsername(userid);
		 req.setMessage("System Message");
		 req.setPassword(password);
		 req.setRequest("property_details");
		 req.setProcess("add");				
		 propertydetailsbean.initPool();
		 propertydetailsbean.setReqParam(req);
		 propertydetailsbean.setProcess("add");
		 Hashtable<Object, Object> property =new Hashtable<Object, Object>();
		 property.put("mid", finalcode);
		 property.put("pid", "type");
		 property.put("pv", "code");
		 propertydetailsbean.setMid(finalcode);
		 propertydetailsbean.setPropertyHash(property);
		 propertydetailsbean.insert();
		 return finalcode;  
	 }
	 
	 public static void main(String[] args) 
	 {
		 try 
		 {
			 Codegenerator code = new Codegenerator();
			 System.out.println(code.findCode("5474"));
		} 
		 catch (Exception e) 
		 {
			e.printStackTrace();
		}
	}
}
